package org.example;

import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.operators.SlotSharingGroup;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.Collector;

import java.util.UUID;

public class App {
    public static void main( String[] args ) throws Exception {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8081);
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);
        /**
         * 使用 StreamExecutionEnvironment#disableOperatorChaining() 关闭连接到一起。
         */
        // env.disableOperatorChaining()
        /**
         *
         */
//        SlotSharingGroup slotSharingGroup = SlotSharingGroup.newBuilder("ssg1")
//                .setCpuCores(1)
//                .setTaskHeapMemoryMB(100)
//                .build();
//        env.registerSlotSharingGroup(slotSharingGroup);
        SingleOutputStreamOperator<String> ssg1 = env.addSource(new RichSourceFunction<String>() {
            @Override
            public void run(SourceContext<String> sourceContext) throws Exception {
//                    executor.scheduleAtFixedRate(new DataGenerator(sourceContext), 0 , 1000L , TimeUnit.MILLISECONDS);
                while (true) {
                    Thread.sleep(1000L);
                    sourceContext.collect(UUID.randomUUID().toString());
                }
            }

            @Override
            public void cancel() {

            }
        });//.slotSharingGroup("ssg1");
        ssg1.print("-----");
//        SingleOutputStreamOperator<Tuple2<String, Integer>> tt = ssg1.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
//            @Override
//            public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
//                String[] split = s.split(",");
//                for (String s1 : split) {
//
//                    if (null != s1 && StringUtils.containsAny(s1 , new String[]{"1111","2222"})) {
//                        collector.collect(new Tuple2<>(s1, 1));
//                    }
//                }
//            }
//        });
//        tt.print();

//        tt.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
//            @Override
//            public String getKey(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
//                return stringIntegerTuple2.f0;
//            }
//        }).sum(1).print("-------");
        //打印输出
        env.execute();
    }
    public static class DataGenerator implements Runnable{
        private SourceFunction.SourceContext<String> sourceContext;
        public DataGenerator(SourceFunction.SourceContext<String> sourceContext){
            this.sourceContext = sourceContext ;
        }

        @Override
        public void run() {
            sourceContext.collect(UUID.randomUUID().toString());
        }
    }
}
